package com.zym.init.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zym.init.core.cache.StorageCache;
import com.zym.init.core.exception.ServiceException;
import com.zym.init.mapper.StorageMapper;
import com.zym.init.model.dto.PageDTO;
import com.zym.init.model.po.StoragePo;
import com.zym.init.model.query.StorageQuery;
import com.zym.init.model.vo.StorageVo;
import com.zym.init.service.StorageService;
import org.springframework.stereotype.Service;

/**
* @author zou
* @description 针对表【storage(存储配置表)】的数据库操作Service实现
* @createDate 2024-11-13 01:13:22
*/
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, StoragePo>
    implements StorageService{

    /**
     * 获取列表
     */
    @Override
    public PageDTO<StorageVo> getPage(StorageQuery query) {
        // 分页查询
        Page<StoragePo> page = lambdaQuery()
                .eq(query.getStorageId() != null, StoragePo::getStorageId, query.getStorageId())
                .like(query.getStorageName() != null, StoragePo::getStorageName, query.getStorageName())
                .page(query.toMpPageDefaultSortByCreateTimeDesc());
        // 封装返回
        return PageDTO.of(page, StorageVo.class);
    }

    /**
     * 新增
     */
    @Override
    public boolean add(StorageQuery query) {
        isStorageNameExist(query.getStorageName());
        StoragePo po = new StoragePo();
        po.setStorageName(query.getStorageName());
        po.setStorageDesc(query.getStorageDesc());
        po.setStorageConfig(query.getStorageConfig());
        po.setStatus(query.getStatus());
        po.setIsDefault(query.getIsDefault());
        po.setServiceProvider(query.getServiceProvider());

        // 判断是否为默认存储
        if (query.getIsDefault()) {
            updateAllDefault();
        }

        boolean save = save(po);
        // 缓存
        StorageCache.set(po.getStorageId());
        return save;
    }

    /**
     * 编辑
     */
    @Override
    public boolean edit(StorageQuery query) {
        QueryWrapper<StoragePo> wrapper = new QueryWrapper<>();
        wrapper.eq("storage_id", query.getStorageId());
        StoragePo po = getOne(wrapper);
        if(po == null){
            throw new ServiceException("存储配置不存在");
        }
        if (!po.getStorageName().equals(query.getStorageName())) {
            isStorageNameExist(query.getStorageName());
        }
        StoragePo storage = new StoragePo();
        storage.setStorageId(query.getStorageId());
        storage.setStorageName(query.getStorageName());
        storage.setStorageConfig(query.getStorageConfig());
        storage.setStorageDesc(query.getStorageDesc());
        storage.setStorageConfig(query.getStorageConfig());
        storage.setStatus(query.getStatus());
        storage.setIsDefault(query.getIsDefault());
        storage.setServiceProvider(query.getServiceProvider());

        // 判断是否为默认存储
        if (query.getIsDefault()) {
            updateAllDefault();
        }

        boolean update = updateById(storage);

        // 缓存
        StorageCache.set(storage.getStorageId());
        return update;
    }

    /**
     * 修改所有配置为非默认
     */
    private void updateAllDefault() {
        QueryWrapper<StoragePo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_default", true);
        StoragePo storage = new StoragePo();
        storage.setIsDefault(false);
        update(storage, wrapper);
    }

    /**
     * 判断存储名称是否存在
     */
    private void isStorageNameExist(String storageName) {
        QueryWrapper<StoragePo> wrapper = new QueryWrapper<>();
        wrapper.eq("storage_name", storageName);
        if (count(wrapper) > 0) {
            throw new ServiceException("存储名称已存在");
        }
    }
}




